package com.ww.springboot.boot.javabase.hashmap.demo;

import com.ww.springboot.boot.utils.Func;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-10-06 16:07
 */
public class HashMapDemo {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();

        //map.put("A","a");
        //无值时才赋值
        String s = map.putIfAbsent("A", "b");
        System.out.println(s);
        s = map.putIfAbsent("A", "c");
        System.out.println(s);
        System.out.println(map);
        //无值时 赋值-》可根据key运算
        map.computeIfAbsent("B", key -> key + "b");
        System.out.println(map);
        //有没有值 都会执行 key-value 都可以利用
        map.compute("C", (t,u) -> t + "/b/" + u);
        System.out.println(map);

        //有值才会执行
        map.computeIfPresent("B",(key,value)-> key + value);
        System.out.println(map);

        Map<String, List<Long>> map2 = new HashMap<>();
        map2.put("B", new ArrayList<>());
        map2.computeIfPresent("B",(key,value)-> {value.add(1L); return value;});
        System.out.println(map2);

        List<Long> longs = Func.toLongList("1,2,3");
        longs.add(1L);
    }
}
